import { baseUrl } from '@/utils/global'
import { getToken } from '@/utils/auth'

const uploadMixin = {
  data() {
    return {
      prefixUrl: process.env.VUE_APP_UPLOAD_PREFIX, // 图片前缀
      uploadUrl: baseUrl + '/admin/file/upload',
      uploadHeaders: {
        token: getToken()
      },
      acceptUploadImg: '.jpg,.jpeg,.png,.gif'
    }
  },
  methods: {
    beforeUpload(file, fileSize = 5) {
      // const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < fileSize
      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      // }
      if (!isLt2M) {
        this.$message.error(`上传图片大小不能超过 ${fileSize}MB!`)
      }
      return isLt2M
    },
    handleUploadError(err, file, fileList) {
      console.log(err, file, fileList)
      this.$message.error('上传失败')
    }
  }
}

export default uploadMixin
